package com.ruoyi.system.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.system.domain.GxDatabase;
import com.ruoyi.system.service.IQuartzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.system.domain.JdbcUtils;
import org.springframework.stereotype.Service;

import java.io.File;
import java.time.LocalDate;
import java.sql.Date;
import java.util.Map;

/**
 * @Author: lsn
 * @ClassName: QuartzServiceImpl
 * @Description:
 * @Data: 2023/7/13 15:44
 **/
@Slf4j
@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private JdbcUtils jdbcUtils;


    public GxDatabase mysqlBackupTask(GxDatabase database) {
        log.info("======开始备份数据库=======");
        // 获取备份地址
        String resourcePath = database.getBackupPath();
        String backUpPath = resourcePath + "/sql/";
        File backUpFile = new File(backUpPath);
        if (!backUpFile.exists()) {
            backUpFile.mkdirs();
        }
        File dataFile = new File(backUpPath + database.getDbname() + System.currentTimeMillis() + ".sql");
        //拼接cmd命令
        StringBuffer sb = new StringBuffer();
        sb.append("mysqldump");
        sb.append(" -u" + database.getUsername());
        sb.append(" -p" + database.getPassword());
        sb.append(" " + database.getDbname() + " > ");
        sb.append(dataFile);
        log.info("======数据库备份cmd命令为：" + sb.toString() + "=======");
        try {
            Process exec = Runtime.getRuntime().exec("cmd /c" + sb.toString());
            if (exec.waitFor() == 0) {
                log.info("======数据库备份成功，路径为：" + dataFile + "=======");
            }
        } catch (Exception e) {
            log.info("======数据库备份失败，异常为：" + e.getMessage() + "=======");
        }
        return database;
    }

    public GxDatabase mysqlRecoveryTask(GxDatabase database) {
        log.info("======开始恢复数据库=======");
        //拼接cmd命令
        StringBuffer sb = new StringBuffer();
        sb.append("mysql");
        sb.append(" -u" + database.getUsername());
        sb.append(" -p" + database.getPassword());
        sb.append(" --default-character-set=utf8");
        sb.append(" " + database.getDbname() + " < ");
        String fileName = database.getFileName();
        String backupPath = database.getBackupPath();
        String dataFile = backupPath + "\\sql\\" + fileName;
        sb.append(dataFile);
        log.info("======数据库恢复cmd命令为：" + sb.toString() + "=======");
        try {
            Process exec = Runtime.getRuntime().exec("cmd /c" + sb.toString());
            if (exec.waitFor() == 0) {
                log.info("======数据库恢复成功");
            }
        } catch (Exception e) {
            log.info("======数据库恢复失败，异常为：" + e.getMessage() + "=======");
        }
        return database;
    }

//    @Override
//    public void mysqlBackupTask() {
//        String resourcePath = "D:\\ruoyi\\mysqlBackup";
//        log.info("======执行定时器:定时备份数据库=======");
//        String backUpPath = resourcePath + "/sql/" + java.sql.Date.valueOf(LocalDate.now());
//        File backUpFile = new File(backUpPath);
//        if (!backUpFile.exists()) {
//            backUpFile.mkdirs();
//        }
//        File dataFile = new File(backUpPath + "/yikatong" + System.currentTimeMillis() + ".sql");
//        //拼接cmd命令
//        StringBuffer sb = new StringBuffer();
//        Map<String, String> dbInfo = jdbcUtils.getDBInfo();
//        sb.append("mysqldump");
//        sb.append(" -u" + dbInfo.get("userName"));
//        sb.append(" -p" + dbInfo.get("passWord"));
//        sb.append(" " + dbInfo.get("dbName") + " > ");
//        sb.append(dataFile);

//        sb.append("mysqldump");
//        sb.append(" -u" + "root");
//        sb.append(" -p" + "lsn123");
//        sb.append(" " + "ruoyi-vue" + " > ");
//        sb.append(dataFile);
//        log.info("======数据库备份cmd命令为：" + sb.toString() + "=======");
//        try {
//            Process exec = Runtime.getRuntime().exec("cmd /c" + sb.toString());
//            if (exec.waitFor() == 0) {
//                log.info("======数据库备份成功，路径为：" + dataFile + "=======");
//            }
//        } catch (Exception e) {
//            log.info("======数据库备份失败，异常为：" + e.getMessage() + "=======");
//        }
//    }

//    @Override
//    public void removeMysqlBackupTask() {
//        log.info("======执行定时器:定时删除备份数据库文件=======");
//        String resourcePath = "D:\\ruoyi\\mysqlBackup";
//        String backUpPath = resourcePath + "/sql";
//        File backUpFile = new File(backUpPath);
//        if (backUpFile.exists()) {
//            File[] files = backUpFile.listFiles();
//            for (File file : files) {
//                if (file.isDirectory()) {
//                    Date date1 = Date.valueOf(file.getName());
//                    Date date2 = Date.valueOf(LocalDate.now());
//                    long betweenDay = DateUtil.between(date1, date2, DateUnit.DAY);
//                    if (betweenDay > 7) {
//                        File[] subFiles = file.listFiles();
//                        for (File subFile : subFiles) {
//                            subFile.delete();
//                        }
//                        file.delete();
//                    }
//                }
//            }
//        }
//    }
}
